<?php 

class Banlist{

	function insertBan($steamID, $reasonID=1){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start insertBan <br>\n";
		if($steamID > 0){

			$insertArray = array();
			$insertArray['SteamID'] = secureNumber($steamID);
			$insertArray['BannedAt'] = time();

			$retTill = $this->calculateBannedTill($steamID);
			$bannedTill = $retTill['data'];

			$insertArray['BannedTill'] = (int) $bannedTill;
			$insertArray['BanlistReasonID'] = (int)$reasonID;
			$insertArray['Display'] = (int)1;

			$retIns = $DB->insert("Banlist", $insertArray);
			$ret['debug'] .= p($retIns,1);

			$ret['status'] = $retIns;
		}
		else{
			$ret['status'] = "SteamID = 0";
		}

		$ret['debug'] .= "End insertBan <br>\n";

		return $ret;
	}

	function calculateBannedTill($steamID){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		$ret['debug'] .= "Start calculateBannedTill <br>\n";
		if($steamID > 0){
			// bereits vorhandene BanCount auslesen
			$retCount = $this->countBans($steamID);
			$count = $retCount['count'];

			switch($count){
				// Verwarnung
				case "0":
					$bannedTill = time();
					break;
					// Queue Ban
				case "1":
					// 24h ban
					$bannedTill = time() + (24 * 60 * 60);
					break;
					// Queue Ban
				case "2":
					// 3 Tage ban
					$bannedTill = time() + (24 * 60 * 60*3);
					break;
					// Queue Ban
				case "3":
					// 7 Tage ban
					$bannedTill = time() + (24 * 60 * 60*7);
					break;
					// 21 Tage Ban
				case "4":
					// 24h ban
					$bannedTill = time() + (24 * 60 * 60*21);
					break;
					// Perma Ban
				case "5":
					// 24h ban
					$bannedTill = 99999999999;
					break;
			}
			$ret['data'] = $bannedTill;
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "SteamID = 0";
		}
			
		$ret['debug'] .= "End calculateBannedTill <br>\n";
			
		return $ret;
	}

	function countBans($steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		$ret['debug'] .= "Start countBans <br>\n";
		if($steamID > 0){
			$sql = "SELECT *
					FROM `Banlist`
					WHERE SteamID = ".secureNumber($steamID)."
							";
			$ret['debug'] .= p($sql,1);
			$data = $DB->multiSelect($sql);
			
			$count = count($data);
			$ret['data'] = $data;
			$ret['count'] = $count;
			$ret['status'] = true;

		}
		else{
			$ret['status'] = "SteamID = 0";
		}

		$ret['debug'] .= "End countBans <br>\n";

		return $ret;
	}

	function checkForBansOfPlayer($steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();

		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		$ret['debug'] .= "Start checkForBansOfPlayer <br>\n";
		if($steamID > 0){
			$countBans = $this->countBans($steamID);
			$ret['debug'] .= p($countBans,1);
			// wenn er mindestens einen Ban hat
			// konntrollieren ob er schon alle abgesessen hat
			if($countBans['count'] > 0){
				$sql = "SELECT *
						FROM `Banlist`
						WHERE SteamID = ".secureNumber($steamID)."
								AND Display = 1
								AND BannedTill > ".time()."
										ORDER BY BannedTill DESC
										LIMIT 1
										";
				$ret['debug'] .= p($sql,1);
				$data = $DB->select($sql);
				$ret['debug'] .= p($data,1);
				// Wenn Datensatz vorhanden -> dann hat ban und darf nicht joinen
				if(is_array($data) && count($data) > 0){
					$ret['debug'] .= p("BANNED!",1);
					$ret['banned'] = true;
				}
				else{
					
					$ret['banned'] = false;
				}
				if($countBans['count'] == 1){
					$ret['display'] = (int)$countBans['data'][0]['Display'];
				}
				$ret['banCounts'] = (int)$countBans['count'];
			}
			else{
				$ret['banCounts'] = (int)$countBans['count'];
				$ret['banCounts'] = (int)$countBans['count'];
				$ret['banned'] = false;
			}
			$ret['status'] = true;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End checkForBansOfPlayer <br>\n";

		return $ret;
	}

	function getCurrentBanDataOfPlayer($steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}

		$ret['debug'] .= "Start getCurrentBanDataOfPlayer <br>\n";
		if($steamID > 0){
			$retBans = $this->checkForBansOfPlayer();
			if($retBans['banned']){
				$sql = "SELECT b.*, br.Name as Reason
						FROM `Banlist` b JOIN BanlistReasons br ON b.BanlistReasonID = br.BanlistReasonID
						WHERE SteamID = ".secureNumber($steamID)."
								AND Display = 1
								AND BannedTill > ".time()."
										ORDER BY BannedTill DESC
										LIMIT 1
										";
				$ret['debug'] .= p($sql,1);
					
				$data = $DB->select($sql);
				$ret['banned'] = true;
				$ret['data'] = $data;
				$ret['status'] = true;
			}
			else{
				$ret['banned'] = false;
				$ret['status'] = false;
			}
				
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End getCurrentBanDataOfPlayer <br>\n";

		return $ret;
	}

	function cleanBansOfPlayer($steamID=0){
		$ret = array();
		$DB = new DB();
		$con = $DB->conDB();
		if($steamID == 0){
			$steamID = $_SESSION['user']['steamID'];
		}
		$ret['debug'] .= "Start cleanBansOfPlayer <br>\n";
		if($steamID > 0){
			$sql = "UPDATE `Banlist`
					SET Display = 0
								WHERE SteamID = ".secureNumber($steamID)."
								";
			$ret['debug'] .= p($sql,1);
			$data = $DB->update($sql);
			$ret['status'] = $data;
		}
		else{
			$ret['status'] = "MatchID = 0";
		}

		$ret['debug'] .= "End cleanBansOfPlayer <br>\n";

		return $ret;
	}

}

?>